package hu.szyrt.bug.utils;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.util.DisplayMetrics;

public class Assets {

	private Context context;
	private AssetManager am;
	private BitmapFactory.Options options;
	private Typeface buttonTypeface;
	private String language;
	
	@SuppressLint("NewApi")
	public Assets(Context context) {
		this.context = context;
		this.am = context.getAssets();
		this.language =Utils.supportedLanguage(Locale.getDefault().getLanguage());
		
		this.options = new BitmapFactory.Options();
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1)
			this.options.inPreferQualityOverSpeed = false;
		this.options.inPreferredConfig = Config.ALPHA_8;
		this.options.inSampleSize = 2;
		this.options.inDither = false;
		
		this.buttonTypeface = Typeface.createFromAsset(am, "fonts/pacifico.ttf");
	}
	
	public Bitmap loadPolaroid(String name, int sampleSize){
		options.inSampleSize = sampleSize + getDPIForSampleSize();
		return loadBitmap("polaroids/" + name, options);		
	}
	
	public Bitmap loadCategoryIcon(String name, int sampleSize){		
		options.inSampleSize = sampleSize + getDPIForSampleSize();
		return loadBitmap("categories/category_" + name, options);
	}
	
	public Bitmap loadHighlights(String name){
		options.inSampleSize = 1;		
		return loadBitmap("highlights-" + language + "/highlights-" + getDPI() +"/" + name + ".png", options); 
	}
	
	private Bitmap loadBitmap(String assetsPath, BitmapFactory.Options options){		
		try{
			return BitmapFactory.decodeStream(am.open(assetsPath, AssetManager.ACCESS_STREAMING), new Rect(0, 0, 0, 0), options);
		}catch (IOException e) {
			return null;
		}
	}	
	
	public Bitmap loadGalleryImage(String fileName){
		options.inSampleSize = 2 + getDPIForSampleSize();
		return BitmapFactory.decodeFile(fileName, options);
	}
	
	
	public static File getCache(){
		if(!hu.szyrt.bug.utils.Config.BUG_CACHE.exists())
			hu.szyrt.bug.utils.Config.BUG_CACHE.mkdirs();
		return hu.szyrt.bug.utils.Config.BUG_CACHE;
	}
	
	public static File getCacheFile(String name){
		return new File(getCache(), name);
	}
	
	private String getDPI(){
		switch(context.getResources().getDisplayMetrics().densityDpi){
			case DisplayMetrics.DENSITY_LOW: return "ldpi";
			case DisplayMetrics.DENSITY_MEDIUM: return "mdpi";
			case DisplayMetrics.DENSITY_HIGH: return "hdpi";
			default: return "hdpi";
		}
	}
	
	private int getDPIForSampleSize(){
		switch(context.getResources().getDisplayMetrics().densityDpi){
			case DisplayMetrics.DENSITY_LOW: return 2;
			case DisplayMetrics.DENSITY_MEDIUM: return 1;
			case DisplayMetrics.DENSITY_HIGH: return 0;
		default: return 1;
	}
	}
	
	// Getters ----------------------------------------------------
	public Context getContext() {
		return context;
	}
	
	public Typeface getButtonTypeface(){
		return this.buttonTypeface; 
	}
	
	
	
}
